package com.gljx.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.web.dao.CommonDao;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.impl.CustomLogServiceImpl;

public class GetCommonFnumber extends CommonDao {

	@Autowired
	private CustomLogService customLogService;
	
	
	
	public String getLastFnumber(String tableName,String companyId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_tableName", tableName);
		paramMap.put("v_funmber", java.sql.Types.VARCHAR);
		customLogService.selectFunmber(tableName,companyId,"x");
		return (String)paramMap.get("v_funmber");
	}
	
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		String className = "com.gljx.util.GetCommonFnumber";
        Class clazz = Class.forName(className);    
        Object object = clazz.newInstance(); 
        GetCommonFnumber getCommonFnumber = (GetCommonFnumber) object;
	}
	/**
	 * 获取编码
	 * @param  tableName 表名 String
	 * @param name 使用者功能名称  String
	 * @author zyq.
	 * @createTime 2017-11-8 9:23 am
	 * @return  Fnumber
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static String getFnumber(String  tableName,String name,String companyId) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		
		  String className = "com.gljx.util.GetCommonFnumber";
	        
	        Class clazz = Class.forName(className);
	        
	        Object object=clazz.newInstance();
	       
	        GetCommonFnumber getCommonFnumber = (GetCommonFnumber) (object);
		
		/*GetCommonFnumber getCommonFnumber=new GetCommonFnumber();*/
		String fnumber=getCommonFnumber.getLastFnumber(tableName,companyId);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		//获取当天
		String time = simpleDateFormat.format(new Date());
		StringBuffer stringBuffer = new StringBuffer();
		String number = null;
		//如果为空说明是第一次,字符串长度小于12
		if(fnumber == null || "".equals(fnumber) || fnumber.length()<12){
			number = "0001";
		}else{
				//取编码最后的12位数
				String fnumberString = fnumber.substring(fnumber.length()-12);
				//取编码时间
				String timeString = fnumberString.substring(0,8);
				//如果是当天就进行+1操作，否则重新生成
				if(timeString.equals(time)){
					//取编码最后的4位数
					String num = fnumberString.substring(fnumberString.length()-4);
					//转成Integer类型
					Integer fnumbers = Integer.valueOf(num);
					//先进行+1操作在转成String
					if(fnumbers < 10){
						  number = "000"+(fnumbers+1); 
					}else if(fnumbers < 100){
						  number = "00"+(fnumbers+1);
					}else if(fnumbers < 1000){
						  number = "0"+(fnumbers+1);
					}else{
						  number = ""+(fnumbers+1);
					}
				}else{
					number = "0001";
				}
		}
		//功能名
		stringBuffer.append(name);
		//时间
		stringBuffer.append(time);
		//编码
		stringBuffer.append(number);
		return stringBuffer.toString();
	} 
	
	
	
}
